// facility/historyInfo/historyInfo.js
import http from '../../http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		iotDeviceId: '',
		infoList: [],

		timeStartShow: false,
		timeEndShow: false,
		timeStart: '',
		timeEnd: '',
		timeText: '请选择时间',
		minDate: new Date(2020, 10, 1).getTime(),
		maxDate: new Date().getTime(),
		timeEndMinDate: new Date().getTime(),
		formatter(type, value) {
			if (type === 'year') {
				return `${value}年`;
			}
			if (type === 'month') {
				return `${value}月`;
			}
			if (type === 'day') {
				return `${value}日`;
			}
			return value;
		},
	},
	gain() {
		wx.showLoading({
			title: '加载中...'
		});
		let start = this.timestampToTime(this.data.timeStart)
		let end = this.timestampToTime(this.data.timeEnd)
		let data = {
			iotDeviceId: this.data.iotDeviceId,
			timestamp: [start + ' 00:00:00', end + ' 23:59:59']
		}
		let data1 = []
		let data2 = []
		let data3 = []
		data.property = 'voltage'
		http.post({
			url: '/zg/iot-links/data/timestamp',
			data: data,
			then: (res) => {
				console.log('历史数据1', res);
				data1 = res.data.data ? res.data.data : []
				data.property = 'electricity'
				http.post({
					url: '/zg/iot-links/data/timestamp',
					data: data,
					then: (res) => {
						console.log('历史数据2', res);
						data2 = res.data.data ? res.data.data : []
						data.property = 'energy'
						http.post({
							url: '/zg/iot-links/data/timestamp',
							data: data,
							then: (res) => {
								console.log('历史数据3', res);
								data3 = res.data.data ? res.data.data : []
								data1.forEach((item1, index1) => {
									data2.forEach((item2, index2) => {
										if ((index1 = index2)) {
											item1.electricity = item2.value;
										}
									});
									data3.forEach((item3, index3) => {
										if ((index1 = index3)) {
											item1.energy = item3.value;
										}
									});
								});
								console.log('历史数据', data1);
								let list = data1.map(item => {
									item.value = Number(item.value).toFixed(2)
									item.electricity = Number(item.electricity).toFixed(2)
									item.power = Number(item.value * item.electricity).toFixed(2)
									item.energy = Number(item.energy).toFixed(2)
									item.createTime = this.timestampToTime1(item.createTime)
									return item
								})
								this.setData({
									infoList: list
								}, () => {
									wx.hideLoading()
								});
							}
						})
					}
				})
			}
		})
	},
	timeBtn() {
		this.setData({
			timeStartShow: true,
		})
	},
	timeStartYes(event) {
		this.setData({
			timeStart: event.detail,
			timeEndMinDate: event.detail,
			timeStartShow: false,
			timeEndShow: true
		});
	},
	timeEndYes(event) {
		let start = this.timestampToTime(this.data.timeStart)
		let end = this.timestampToTime(event.detail)
		this.setData({
			timeEnd: event.detail,
			timeText: start + ' 至 ' + end,
			timeEndShow: false,
		}, () => {
			this.gain()
		})
	},
	timeonClose() {
		this.setData({
			timeStartShow: false,
			timeEndShow: false,
			// timeStart: '',
			// timeEnd: '',
			// timeText: '请选择时间',
		})
	},
	// 时间戳转换
	timestampToTime(timestamp) {
		if (timestamp) {
			var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-';
			var M = date.getMonth() + 1 + '-';
			var D = date.getDate();
			let strDate = Y + M + D;
			return strDate;
		} else {
			return '';
		}
	},
	// 时间戳转换
	timestampToTime1(timestamp) {
		if (timestamp) {
			var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-';
			var M = date.getMonth() + 1 + '-';
			var D = date.getDate() + ' ';
			var h =
				date.getHours() < 10 ?
				"0" + date.getHours() + ":" :
				date.getHours() + ":";
			var m =
				date.getMinutes() < 10 ?
				"0" + date.getMinutes() + ":" :
				date.getMinutes() + ":";
			var s =
				date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
			let strDate = Y + M + D + h + m + s;
			return strDate;
		} else {
			return '';
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		let pages = getCurrentPages()
		let currentPage = pages[pages.length - 1]
		let options = currentPage.options

		let endDate = new Date();
		let oneDay = 30 * 24 * 60 * 60 * 1000;
		let startDate = new Date(Date.now() - oneDay);
		this.setData({
			iotDeviceId: options.id,
			timeStart: startDate,
			timeEnd: endDate,
			timeText: this.timestampToTime(startDate) + ' 至 ' + this.timestampToTime(endDate),
		}, () => {
			this.gain()
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})